package com.purong.purong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.purong.purong.R;
import com.purong.purong.bean.BankLoanIntentBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zengwenjie on 2017/12/8.
 */
public class BankLoanIntentAdapter extends RecyclerView.Adapter<BankLoanIntentAdapter.ViewHolder> {
    private List<BankLoanIntentBean> recordList=new ArrayList<>();
    private Context mContext;
    private OnLoanIntentListener mListener;
    public BankLoanIntentAdapter(Context context) {
        this.mContext = context;
    }
    public void setOnLoanIntentListener(OnLoanIntentListener listener){
        this.mListener=listener;
    }
    public void setData(List<BankLoanIntentBean> list) {
        this.recordList = list;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.loan_intention_item, parent, false);
        // set the view's size, margins, paddings and item_circle parameters
        ViewHolder vh = new ViewHolder(v);

        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.date.setText("申请时间: "+recordList.get(position).getCreate_time());
        holder.money.setText(recordList.get(position).getMoney());
        holder.status.setText(recordList.get(position).getIntention_status_name());
        switch (recordList.get(position).getIntention_status()){
            case "1":
                holder.status.setTextColor(mContext.getResources().getColor(R.color.green));
                holder.moreIcon.setImageResource(R.drawable.profile_icon_green);
                break;
            case "2":
                holder.status.setTextColor(mContext.getResources().getColor(R.color.blue));
                holder.moreIcon.setImageResource(R.drawable.profile_icon_blue);
                break;
            case "3":
                holder.status.setTextColor(mContext.getResources().getColor(R.color.text_gray));
                holder.moreIcon.setImageResource(R.drawable.profile_icon_gray);

                break;

        }

        holder.note.setText("说明: "+recordList.get(position).getNote());
        status(holder,recordList.get(position));
        final boolean[] hasCount = {false};
        ViewTreeObserver vto = holder.note.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (hasCount[0])
                    return;
                TextPaint mTextPaint = holder.note.getPaint();
                mTextPaint.setTextSize(holder.note.getTextSize());
                int mTextViewWidth = (int) mTextPaint.measureText(recordList.get(position).getNote());
//                holder.note.setText(recordList.get(position).getNote());
                if (mTextViewWidth > holder.note.getWidth()) {//超出一行
                    recordList.get(position).setNoteSingleLine(false);
                } else {
                    recordList.get(position).setNoteSingleLine(true);
                }
                status( holder,recordList.get(position));
                hasCount[0] =true;
            }
        });

        holder.open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recordList.get(position).setNoteOpen(true);
                status( holder,recordList.get(position));

            }
        });
        holder.close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recordList.get(position).setNoteOpen(false);
                status( holder,recordList.get(position));
            }
        });
        holder.topLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener!=null){
                    mListener.onIntentClick(position);
                }
            }
        });
        if (position==0){
            holder.topView.setVisibility(View.VISIBLE);
        }else {
            holder.topView.setVisibility(View.GONE);
        }
    }
    private void status(ViewHolder holder,BankLoanIntentBean bean){
        if (bean.isNoteSingleLine()){
            holder.openImg.setVisibility(View.GONE);
            holder.close.setVisibility(View.GONE);
        }else {
            if (bean.isNoteOpen()){
                holder.openImg.setVisibility(View.GONE);
                holder.close.setVisibility(View.VISIBLE);
            }else {
                holder.openImg.setVisibility(View.VISIBLE);
                holder.close.setVisibility(View.GONE);
            }

        }
    }
    public  class ViewHolder extends RecyclerView.ViewHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        private TextView money,date,status,note;
        private ImageView openImg,moreIcon;
        private LinearLayout close,open,topLay;
        private View topView;
        public ViewHolder(View v) {
            super(v);
            topLay=(LinearLayout)v.findViewById(R.id.top_lay);
            topView=(View)v.findViewById(R.id.top_view);
            open=(LinearLayout) v.findViewById(R.id.open);
            openImg=(ImageView)v.findViewById(R.id.open_img);
            moreIcon=(ImageView)v.findViewById(R.id.more_icon);
            note=(TextView)v.findViewById(R.id.note);
            money=(TextView)v.findViewById(R.id.money);
            close=(LinearLayout)v.findViewById(R.id.close);
            date=(TextView)v.findViewById(R.id.date);
            status=(TextView)v.findViewById(R.id.status);



        }
    }
    @Override
    public int getItemCount() {
        return recordList.size();
    }
    public interface  OnLoanIntentListener{
        void onIntentClick(int position);
    }
}
